
function $ajax(url, data, fun) {
	var d = eval(data);
	$.post(
		url,d,function(data){
			var json=$.parseJSON(data);
			fun=eval(fun);
			try{fun(json);}catch(e){};
		}
	);
}

/* 表单ajax请求的统一接口 */
$.fn.$send = function(options){
	var setting = $.extend({
		id:'id',
		call:'call'
	},options);
	var form = $("#"+setting.id);
	form.submit(function(){return false});
	var queryString = '{' + form.$formValue() + '}';
	var url = $.trim(form.attr("action"));
	var query = eval('(' + queryString + ')');
	$ajax(url,query,setting.call);
	return false;
}

/* 获取表单数据 */
$.fn.$formValue = function(options){
	var a = [];
	//input
	var form = this[0].getElementsByTagName('input');
	for(var i=0,max = form.length; i<max; i++ ){
		var el = form[i];
		if(el.type == 'checkbox' ){
			if(el.checked == true){
			//	el.value = 'checked';
				a.push('"'+encodeURIComponent(el.name)+'":"'+encodeURIComponent(el.value)+'"');
			}
		}else if(el.type == 'radio'){
			if (el.checked == true){
				a.push('"'+encodeURIComponent(el.name)+'":"'+encodeURIComponent(el.value)+'"');
			}
		}else
		a.push('"'+encodeURIComponent(el.name)+'":"'+encodeURIComponent(el.value)+'"');
	}
	//textarea
	var textarea = this[0].getElementsByTagName("textarea");
	if(textarea.length == 1){
		a.push('"'+encodeURIComponent($(textarea).attr("name"))+'":"'+encodeURIComponent($(textarea).val())+'"');
	}else if(textarea.length >1){
		for(var j=0;max=textarea.length;j++){
			a.push('"'+encodeURIComponent($(textarea[i]).attr("name"))+'":"'+encodeURIComponent($(textarea[i]).val())+'"');
		}
	}
	//select的值
	var select = this[0].getElementsByTagName("select");
	$.each(select,function(i,item){
		a.push('"'+encodeURIComponent($(item).attr("name"))+'":"'+encodeURIComponent($(item).val())+'"');
	});
	return a;
}
/*---------------------------------------------------------------------------------------------------------------------*/

function showloading(display,wating){
	var display = display?display:'block';
	var wating = wating?wating:'Loading...';
	$("#ajaxwaitid").html(wating);
	$("#ajaxwaitid").css("display",display);
}
function addError(text){
	$("#message-text").text(text);
	$("#message-dock").fadeIn();
}
function closeError(){
	$("#message-dock").fadeOut(1000);
}
/*---------------------------------------------------------------------------------------------------------------------*/
jQuery.fn.LoadImage=function(scaling,width,height,loadpic,show){
    if(loadpic==null)loadpic="/images/loading.gif";
	if(show==null)show=true;else show = false;
	return this.each(function(){
		var t=$(this);
		var src=$(this).attr("src")
		var img=new Image();
		//alert("Loading...")
		img.src=src;
		//自动缩放图片
		var autoScaling=function(){
			if(scaling){
			
				if(img.width>0 && img.height>0){ 
			        if(img.width/img.height>=width/height){ 
			            if(img.width>width){ 
			                t.width(width); 
			                t.height((img.height*width)/img.width); 
			            }else{ 
			                t.width(img.width); 
			                t.height(img.height); 
			            } 
			        } 
			        else{ 
			            if(img.height>height){ 
			                t.height(height); 
			                t.width((img.width*height)/img.height); 
			            }else{ 
			                t.width(img.width); 
			                t.height(img.height); 
			            } 
			        } 
			    } 
			}	
		}
		//处理ff下会自动读取缓存图片
		if(img.complete){
		    //alert("getToCache!");
			autoScaling();
		    return;
		}
		$(this).attr("src","");
		//var loading=$("<img alt=\"加载中...\" title=\"图片加载中...\" src=\""+loadpic+"\" />");
		
		t.hide();
		//t.after(loading);
		$(img).load(function(){
			autoScaling();
			//loading.remove();
			t.attr("src",this.src);
			if(show){
				t.show();
			}
			//alert("finally!")
		});
		
	});
}
String.prototype.ncSplit = function(separator){ //按separator分割字符串到数组里，并去掉空的串
		var temp = this.split(separator);
		var tempArray = new Array();
		for(i in temp){
			if(temp[i] && (typeof temp[i] !=='function')){
				tempArray.push(temp[i]);
			}
		}
		return tempArray;
	}
Array.prototype.removeItem=function(str){//删除数组中存在str值的单元
	var temp = this.concat();
	length=temp.length;
	//alert(length);
	this.length=0;
	for(item in temp){
		//alert(temp[item]);
		if(temp[item] == str)continue;
		if(typeof temp[item] === 'function')continue;
		this.push(temp[item]);
	}
	return this;
}

//rel 存放 ID 取ID的html，否取title的内容
$.fn.ncTips = function(){
	return this.each(function(){
		$('<div class="nc-tips"><div class="nc-tip"></div><div class="nc-tip-wrapcontent"><div class="nc-tip-content"></div></div></div>').appendTo('body');
		$(this).mouseover(function() {
			$('.nc-tips').css('visibility', 'visible');
			var ncTips = this;
			var id = ncTips.rel;
			if(id){
				ncHtml = $("#"+id).html();
			}else{
				ncHtml = $(ncTips).attr('data');
			}
			$('.nc-tip-content').html(ncHtml);
		}).mousemove(function(e) {
			$('.nc-tips').css('top', e.pageY + 15);
			$('.nc-tips').css('left', e.pageX - 30);
		}).mouseout(function() {
			$('.nc-tips').css('visibility', 'hidden');
		})
	});				
}
$.fn.inputText = function(){
	return this.each(function(){
		var $this = $(this);
		var oldVal = $this.val();
		if(oldVal){
			this.style.color="#bbb";
			$this.bind("focusin",function(){
				var newVal = this.value;
				if(newVal==oldVal){this.value='';this.style.color="#222";}
			}).bind("focusout",function(){
				var newVal = this.value;
				if(!newVal){this.value=oldVal;this.style.color="#bbb"}
			});
		}
	});
}
;(function($){

	$.EventsStack = [];

	$.EventManager = null;

	$.isLogin = false;

	//注册所有bind的事件
	$.fn.bindAfterLogin = function(type ,data){
		return this.each(function(){
			$this = $(this);
			//register events to events stack;
			if(this.id){//没有这行代码会引起很大的BUG的，如果没有指定ID时就会出现问题，刘浩困我好久，日！！！
				$.EventsStack.push(this.id);
				$this.data("click" , data);
				if($.isLogin){
					$this.bind(type, data);
				}else{
					$this.bind(type , function(){
						login(this.id);
					});
				}
			}
		});
	};

	//注册所有onclick事件
	$.fn.checkClick = function(){
		return this.each(function(){
			$this = $(this);
			data = $this.attr("onclick");
			$this.data("click" , data);
			if(!$.isLogin){
				$this.attr("onclick" , "");
				$this.bind('click' , function(){
					login(this.id);
				});
			}
			$.EventsStack.push(this.id);
		});
	}

	$.rebindEvents = function(){
		for(i in $.EventsStack){
			$this = $("#" + $.EventsStack[i]);
			$this.unbind();
			$this.bind("click" , eval($this.data("click")));
		}
	}


	$.setLogin = function(flag){
		$.isLogin = flag ? true : false;
		return this;
	}


})(jQuery);
//错误
$.fn.ncError = function(){
	return this.each(function(){
		$(this).bind("nc.error",function(event,text){
			$("#message-text").text(text);
			$("#message-dock").fadeIn();
		}).bind("nc.errorclose",function(){
			$("#message-dock").fadeOut(1000);
		});
	});
}
function login(id){
	var html = '<div class="main_con" style="width:350px" id="login">'+
                '<form class="olduser_login_form" id="login-form" action="/account/ajaxlogin"method="post">'+
                   ' <div class="info_con">'+
                      '  <div class="info_title">'+
                           ' <img src="../images/sign_info.jpg" />'+
                          '  <span class="title">'+
                               ' 请输入您的用户名密码'+
                            '</span>'+
                       ' </div>'+
                      '  <div class="name_col">'+
                           ' <label class="name required" for="AccountForm_email">'+
                              '  邮箱'+
                               ' <span class="required">'+
                               '     *'+
                              '  </span>'+
                          '  </label>'+
                          '  <input class="name_txt" value="@" name="AccountForm_email" id="AccountForm_email"'+
                           ' type="text" />'+
                           ' <div id="error_email" class="errorMessage" style="display:block;padding-left:52px;">'+
                           ' </div>'+
                       ' </div>'+
                      '  <div class="psw_col">'+
                           ' <label class="password required" for="AccountForm_password">'+
                                '密码'+
                               ' <span class="required">'+
                                    '*'+
                               ' </span>'+
                            '</label>'+
                            '<input class="psw_txt" name="AccountForm_password" id="AccountForm_password"'+
                            'type="password" value="" />'+
                            '<div id="error_password" class="errorMessage" style="display:block;padding-left:52px;">'+
                            '</div>'+
                       ' </div>'+
                        '<div class="remeb_col">'+
                           ' <input id="ytAccountForm_is_remember" type="hidden" value="0" name="AccountForm_is_remember"'+
                            '/>'+
                            '<input name="AccountForm_is_remember" id="AccountForm_is_remember" value="1"'+
                            'type="checkbox" />'+
                           ' <label for="AccountForm_is_remember">'+
                                '保持登录'+
                            '</label>'+
                           ' <a href="/account/forget">'+
                                '忘记密码了？'+
                            '</a>'+
                        '</div>'+
						'<input type="hidden" name="domid" id="domid" value="'+id+'" />'+
                    '</div>'+
                    '<input class="login_subm" onclick=$(this).$send({"id":"login-form","call":"logined"}); type="submit" name="yt0" value="登录" />'+
                '</form>'+
            '</div>';
	$.fancybox({
		'content':html,
		'type':'html'
		//'hideOnOverlayClick': false,
	});
	$('#login-form').find('input').inputText();
}
function logined(json){
	$this.ncError().trigger('nc.errorclose');
	if(json.flag){
		$.fancybox.close();
		//header();
		$.rebindEvents();
		$("#" + json.id).trigger("click");
	}else{
		var message = '';
		for(i in json.info){
			message += json.info[i];
		}
		$this.ncError().trigger('nc.error',message);
	}
}
function header(){
//	var headinfo = $("#headinfo");
//	headinfo.html("<img src='/images/loading1.gif'><span style='line-height:16px;height:16px;display;float:right;display:block;'>正在加载...</span>");
	//$.get("/index/header",function(data){headinfo.html(data)});
}

function renren_logout(){
	window.location.href = '/account/logout';
}